package org.easydarwin.blogdemos;

import android.content.Context;
import android.media.AudioManager;
import android.util.Log;

import static android.content.Context.AUDIO_SERVICE;

/**
 * Created by Administrator on 2019/8/26 0026.
 */

public class AudioManagerUtils {
    private static AudioManagerUtils manager;
    private AudioManager audioManager;

    private String TAG = getClass().getSimpleName();

    public static AudioManagerUtils getInstance(Context context) {
        if (manager == null) {
            synchronized (AudioManagerUtils.class) {
                if (manager == null) {
                    manager = new AudioManagerUtils(context);
                }
            }
        }
        return manager;
    }

    private AudioManagerUtils(Context context) {
        audioManager = (AudioManager) context.getSystemService(AUDIO_SERVICE);
    }

    public void setStreamVolume(final int volume) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                audioManager.setStreamVolume(AudioManager.STREAM_SYSTEM, volume, AudioManager.FLAG_PLAY_SOUND);
            }
        }).start();
    }

    public void setTipStreamVolume(final int volume) {
        /**
         * @howard 20210611
         * 放入子线程处理，防止ANR
         */
        new Thread(new Runnable() {
            @Override
            public void run() {
                audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, volume, AudioManager.FLAG_PLAY_SOUND);
            }
        }).start();

    }

    public void setsetSpeakerphoneOn() {
        if (!audioManager.isSpeakerphoneOn()) {
            audioManager.setSpeakerphoneOn(true);
        }
    }

    public int getCurrentVoice() {
        Log.d(TAG, "getCurrentVoice==: " + audioManager.getStreamVolume(AudioManager.STREAM_SYSTEM));
        return audioManager.getStreamVolume(AudioManager.STREAM_SYSTEM);
    }

    public void setDefoutMode() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                audioManager.setMode(AudioManager.MODE_NORMAL);
                Log.d(TAG, "setDefoutMode==: " + audioManager.getMode());
            }
        }).start();
    }

    public void setCommunicateMode() {
        audioManager.setMode(AudioManager.STREAM_VOICE_CALL);
        Log.d(TAG, "setDefoutMode==: " + audioManager.getMode());
    }

    //输入音量
    public void setinputStreamVolume(final int volume) {
        /**
         * @howard 20210611
         * 放入子线程处理，防止ANR
         */
        new Thread(new Runnable() {
            @Override
            public void run() {
                int setVol = 0;
                if (volume > 15) {
                    setVol = 15;
                } else if (volume <= 0) {
                    setVol = 0;
                } else {
                    setVol = volume;
                }
                audioManager.setParameters("audio_in_volume=" + setVol);
            }
        }).start();
    }

    public void setParameters(String type) {
        audioManager.setParameters(type);
    }
}
